package ynu.edu.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import ynu.edu.po.Cart;
import ynu.edu.service.ICartService;

import java.util.List;

@RestController
@RequestMapping("/cart")
@Tag(name = "购物车服务")
public class CartController {
    @Resource
    private ICartService cartService;

    @PostMapping("/listCart")
    @Operation(summary = "查询此用户购物车信息",
            description = "根据用户编号查询此用户所有购物车信息；" +
                    "根据用户编号和商家编号，查询此用户购物车中某个商家的所有购物车信息")
    @Parameters({
            @Parameter(name = "userId", description = "用户编号"),
            @Parameter(name = "businessId", description = "商家编号（可选）")
    })
    public List<Cart> listCart(@RequestParam String userId,
                               @RequestParam(required = false) Integer businessId) {
        Cart cart = Cart.builder()
                .userId(userId)
                .businessId(businessId)
                .build();
        return cartService.listCart(cart);
    }

    @PostMapping("/saveCart")
    @Operation(summary = "商品加入购物车",
            description = "向购物车表中添加一条记录")
    @Parameters({
            @Parameter(name = "userId", description = "用户编号"),
            @Parameter(name = "businessId", description = "商家编号"),
            @Parameter(name = "foodId", description = "食物编号")
    })
    public boolean saveCart(@RequestParam String userId,
                            @RequestParam Integer businessId,
                            @RequestParam Integer foodId) {
        Cart cart = Cart.builder()
                .userId(userId)
                .businessId(businessId)
                .foodId(foodId)
                .build();
        return cartService.saveCart(cart);
    }

    @PostMapping("/updateCart")
    @Operation(summary = "更新购物车信息",
            description = "根据用户编号、商家编号、食品编号更新数量")
    @Parameters({
            @Parameter(name = "userId", description = "用户编号"),
            @Parameter(name = "businessId", description = "商家编号"),
            @Parameter(name = "foodId", description = "食物编号"),
            @Parameter(name = "quantity", description = "数量")
    })
    public boolean updateCart(@RequestParam String userId,
                              @RequestParam Integer businessId,
                              @RequestParam Integer foodId,
                              @RequestParam Integer quantity) {
        Cart cart = Cart.builder()
                .userId(userId)
                .businessId(businessId)
                .foodId(foodId)
                .quantity(quantity)
                .build();
        return cartService.updateCart(cart);
    }

    @PostMapping("/removeCart")
    @Operation(summary = "删除购物车信息",
            description = "根据用户编号、商家编号、食品编号删除购物车表中的一条食品记录；" +
                    "根据用户编号、商家编号删除购物车表中的多条记录")
    @Parameters({
            @Parameter(name = "userId", description = "用户编号"),
            @Parameter(name = "businessId", description = "商家编号"),
            @Parameter(name = "foodId", description = "食物编号（可选）")
    })
    public boolean removeCart(@RequestParam String userId,
                              @RequestParam Integer businessId,
                              @RequestParam Integer foodId) {
        Cart cart = Cart.builder()
                .userId(userId)
                .businessId(businessId)
                .foodId(foodId)
                .build();
        return cartService.removeCart(cart);
    }

}
